package com.zh.common.utils.toast

import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.ViewUtils
import com.zh.common.R
import com.zh.common.ktx.dp2px

internal class BkjToast : IToast {

    override fun showShort(message: String?, isCenter: Boolean, icon: Int?) {
        val toast = ToastUtils.make()
        if (isCenter) {//居中 - 可以自定义样式
            toast.setGravity(Gravity.CENTER, 0, 0)
        } else {
            toast.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 100f.dp2px)
        }
        toast.show(makeView(message, icon))
    }

    override fun showLong(message: String?, isCenter: Boolean, icon: Int?) {
        val toast = ToastUtils.make()
        if (isCenter) {//居中 - 可以自定义样式
            toast.setGravity(Gravity.CENTER, 0, 0)
        } else {
            toast.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 100f.dp2px)
        }
        toast.show(makeView(message, icon))
    }

    private fun makeView(message: String?, icon: Int?): View {
        val view = ViewUtils.layoutId2View(R.layout.view_toast)
        val tvContent = view.findViewById<TextView>(R.id.tvContent)
        val ivTis = view.findViewById<ImageView>(R.id.ivTis)
        ivTis.isVisible = false
        tvContent.text = message ?: "null"
        icon?.let {
            ivTis.isVisible = true
            ivTis.setImageResource(icon)
        }
        return view
    }

    override fun cancel() {
        ToastUtils.cancel()
    }
}